package com.cooge.jiexi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

	public static String[] getIPs(String str) throws Exception {

		ArrayList<String> list = new ArrayList<String>();
		String ss[] = str.replaceAll("\r\n", " ").split(" ");
		for(int i = 0;i<ss.length;i++){
			if(isboolIp(ss[i])){
				list.add(ss[i]);
			}
		}
		return list.toArray(new String[]{});
	
	}
	public static boolean isboolIp(String ipAddress)   
	{   
	       String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";    
	       Pattern pattern = Pattern.compile(ip);    
	       Matcher matcher = pattern.matcher(ipAddress);    
	       return matcher.matches();    
	}
	
	    public static <K, V extends Comparable<? super V>> Map<K, V> 
	        sortByValue( Map<K, V> map )
	    {
	        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>( map.entrySet() );
	        Collections.sort( list, new Comparator<Map.Entry<K, V>>()
	        {
	            public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 )
	            {
	                return (o2.getValue() ).compareTo( o1.getValue() );
	            }
	        } );

	        Map<K, V> result = new LinkedHashMap<K, V>();
	        for (Map.Entry<K, V> entry : list)
	        {
	            result.put( entry.getKey(), entry.getValue() );
	        }
	        return result;
	    }

}
